package com.luculent.util.extension.datamasking.ser;

import com.fasterxml.jackson.databind.BeanProperty;
import com.luculent.util.extension.datamasking.annotation.Sensitive;

import java.util.Collection;
import java.util.Objects;

/**
 * 序列化策略工厂
 * @author zhangyang
 * @date 2020/11/3 17:43
 */
public class SensitiveSerializerFactory {

    private SensitiveSerializerFactory() {
        throw new AssertionError();
    }

    public static BaseSensitiveSerializer createSerializer(Sensitive sensitiveInfo, BeanProperty property) {
        if (sensitiveInfo != null) {
            boolean isRawClass = Collection.class.isAssignableFrom(property.getType().getRawClass()) && property.getMember().getType().getContentType().getRawClass() == String.class;
            if (isRawClass) {
                return new CollectionSensitiveSerializer(sensitiveInfo);
            } else if (Objects.equals(property.getType().getRawClass(), String[].class)) {
                return new ArraySensitiveSerializer(sensitiveInfo);
            } else if (Objects.equals(property.getType().getRawClass(), String.class)) {
                return new StringSensitiveSerializer(sensitiveInfo);
            }
        }
        return null;
    }


}
